package com.jctrip.hotel.source.xxl.greentree.thread;

import com.jctrip.hotel.common.constant.RedisKeyConstant;
import com.jctrip.hotel.common.context.BeanContext;
import com.jctrip.hotel.common.util.RedisUtil;
import com.jctrip.hotel.source.service.greentree.GreenTreeTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

/**
 * @Author：Haruhi
 * @description:
 * @Date：2023/9/4 11:17
 * @Package：com.jctrip.hotel.source.xxl.greentree.thread
 * @Project：hotel-main
 */
@Slf4j
public class SyncGreenTreeHotelIdTaskThread implements Runnable{
    private final String chainCode;

    public SyncGreenTreeHotelIdTaskThread(String chainCode) {
        this.chainCode = chainCode;
    }

    @Override
    public void run() {
        try {
            RedisUtil redisUtil = BeanContext.getApplicationContext().getBean(RedisUtil.class);
            GreenTreeTaskService greenTreeTaskService = BeanContext.getApplicationContext().getBean(GreenTreeTaskService.class);

            while (redisUtil.lGetListSize(RedisKeyConstant.HOTEL_MAIN_GREENTREE_HOTELID_LIST) > 0) {
                try {
                    String hotelId = (String) redisUtil.rightPop(RedisKeyConstant.HOTEL_MAIN_GREENTREE_HOTELID_LIST);
                    if (StringUtils.isBlank(hotelId)) {
                        continue;
                    }

                    greenTreeTaskService.syncGreenTreeHotelIdListTask(chainCode,hotelId);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
